package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	

	import com.housediyadmin.events.buildingEvents.DeleteBuildingEvent;
	import com.housediyadmin.events.buildingEvents.EditBuildingEvent;
	import com.housediyadmin.events.buildingEvents.GetBuildingEvent;
	import com.housediyadmin.events.houseEvents.DeleteHouseEvent;
	import com.housediyadmin.events.houseEvents.GetHousesEvent;
	import com.housediyadmin.model.AreaTreeModel;
	import com.housediyadmin.model.BuildingModel;
	import com.housediyadmin.model.HouseModel;
	import com.housediyadmin.vo.Building;
	import com.housediyadmin.vo.House;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.CheckBox;
	
	public class RentererBuildingMediator extends Mediator
	{
		[Inject] 
		public var view:RentererBuildingView;

		[Inject]
		public var treeModel:AreaTreeModel;
		
		[Inject]
		public var houseModel:HouseModel;
		
		[Inject]
		public var buildingTreeModel:AreaTreeModel;

		[Inject]
		public var buildingModel:BuildingModel;
		
		override public function onRegister():void
		{
			view.delBtn.addEventListener(MouseEvent.CLICK,deleteEve);
			view.modifyBtn.addEventListener(MouseEvent.CLICK,modify);
			view.displayHouseBtn.addEventListener(MouseEvent.CLICK, displayHouseResult);
			view.seleCheckBox.addEventListener(MouseEvent.CLICK,selected);
		}
		
		private function selected(e:MouseEvent):void
		{
			if(e.target.selected)
			{
				buildingModel.selectedBuildingArr.push(buildingModel.selectedBuilding);
				buildingModel.selectedBuilding.isSelected=true;
			}
			else
			{
				var index:int=buildingModel.selectedBuildingArr.indexOf(buildingModel.selectedBuilding);
				buildingModel.selectedBuildingArr.splice(index,1);
				buildingModel.selectedBuilding.isSelected=false;
			}
		}
		
		public var buildingIdsAC:ArrayCollection=new ArrayCollection();
		private function deleteEve(e:MouseEvent):void
		{
			
			buildingIdsAC.addItem(buildingModel.selectedBuilding.buildingID);
			dispatch(new GetHousesEvent(GetHousesEvent.GET_HOUSE_IDS,buildingIdsAC));
			
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("确定删除？","",3,view,deleteAlert);
			
		}
		
		private function deleteAlert(e:CloseEvent):void
		{
			if(e.detail==Alert.YES)
			{
				var areaIds:Array=new Array();
				var buildingIdsAC:ArrayCollection=new ArrayCollection();
				buildingIdsAC.addItem(buildingModel.selectedBuilding.buildingID);
				dispatch(new GetHousesEvent(GetHousesEvent.GET_HOUSE_IDS,buildingIdsAC));
				
				//house的处理
				if(houseModel.houseArray && houseModel.houseArray.length!=0)
				{
					Alert.show("删除失败。楼盘下还有未删除的房型信息！");
					return;
				}
				
				areaIds.push(buildingModel.selectedBuilding.areaID);
				dispatch(new DeleteBuildingEvent(DeleteBuildingEvent.DELETE_BUILDING,buildingIdsAC.source,areaIds));
			}
		}
		
		private function modify(e:MouseEvent):void
		{
			var popup:EditBuildingView=new EditBuildingView();
			popup.type="EDIT";
			PopUpManager.addPopUp(popup,contextView,true);
			PopUpManager.centerPopUp(popup);
			mediatorMap.createMediator(popup);
		}
		
		private function displayHouseResult(event:MouseEvent):void
		{
			var buildingID:int = buildingModel.selectedBuilding.buildingID;
			var buildingIDs:ArrayCollection = new ArrayCollection();
			buildingIDs.addItem(buildingID);
			dispatch(new GetHousesEvent(GetHousesEvent.GET_HOUSE_IDS, buildingIDs, null,null));	
		}
	}
}